define(function(require, exports, module) {
  var $ = require("$");
  var bootbox = require("bootbox");
  var dtUtil = require('datatableutil');
  var nav = require("bootstrap_nav");
  var loader = require("./loader/loader");
  require("bootstrap_commonrequire");
  require("moment");
  require('daterangepicker.css');
  require('bootstrap-daterangepicker');
  require('select2.css');
  require('select2');

  exports.init = function(param) {
    
    $(document).ready(function() {
      //父节点，单选按钮的name值
      var parent="";
      //当前节点，单选按钮的value值
      var myId="";
      // 点击单选按钮时，弹出的确认对话框的确认值
      var radioUsing = "no";
      // 父节点
      var parentId = -1;
      
      // 加载自定义模板-条件区域
      var template = require("./ruleinfo.tpl");
      var Widget = require("widget");
      var Component = Widget.extend({
        template : template,
      });
      new Component({
        renderTo : "queryAreaId"
      });
      
      //加载表单模板和数据
      var Table = require("table");
      // 查询内容显示
      new Table({
        attrs : {
          id : "data_table",
          isHovered : false,
          isStriped : true,
          isBordered : false
        },
        renderTo : param.divWrapperId

      });
      // 规则信息显示
      var ruleinfo_data_table = $('#data_table').dataTable({
        "sDom" : "<'hidden'<'span6'l><'hidden'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
        "bJQueryUI" : false,
        "oLanguage" : {
          "sUrl" : "/business/js/common/table/src/lang/dataTable.cn.txt"
        },
        "sPaginationType" : "bootstrap", // 分页自带两种样式，full_numbers和two_button(默认)
        'bPaginate' : false, // 是否分页
        "bProcessing" : false, // 当datatable获取数据时候是否显示正在处理提示信息。
        "bServerSide" : false, // 指定服务器端分页
        "bDeferRender" : true,
        "bFilter" : true,
        "bSort" : false,
        "bInfo" : false,//不显示“当前显示 1 到 8 条，共 8 条记录”这句话
        "sAjaxSource" : param.ajaxUrl,
        "iDisplayLength" : DATATABLES_CONFIG.iDisplayLength.DEFAULT, // 默认分页的每页记录数
        "fnServerData" : function(sSource, aoData, fnCallback, oSettings) {
          var queryparam = dtUtil.convert_aoData(aoData);
          // 添加自己的额外参数
          oSettings.jqXHR = $.ajax({
            "dataType" : 'json',
            "contentType" : "application/json;charset=UTF-8",
            "type" : "GET",
            "url" : sSource,
            "data" : queryparam, // 将请求数据转换为后台要求的JSON格式
            "success" : function(json) {
              if (json.flag == '000000') {
                dtUtil.successCallback(json, aoData, fnCallback);
                
              } else {
                oauth2Relogin();
              }
            }
          });

        },
        "aoColumns" : [ {
          "sTitle" : "id",
          "bSortable" : false,
          "mData" : "id",
          "bVisible" : false,
          "mRender" : function(data, status, full) {
            myId=data;
            return myId;
          }
        }, {
          "sTitle" : "父节点",
          "bSortable" : false,
          "mData" : "parent_id",
          "bVisible" : false,
          "mRender" : function(data, status, full) {
            if(data != null){
              parentId = data;
              if(parentId==-1){
                parent="";
              }else{
                parent=parentId+"";
              }
            }else{
              parentId = -1;
            }
            return parentId;
          }
        }, {
          "sTitle" : "规则标识",
          "bSortable" : false,
          "mData" : "type",
          "mRender" : function(data, status, full) {
            var strType = data;
            if(parentId==-1){
              return '<h4><b>'+strType+'</b></h4>';
            }else{
              return strType;
            }
          }
        },{
          "sTitle" : "能力名称",
          "bSortable" : false,
          "mData" : "names",
          "mRender" : function(data, status, full) {
            var strName = data;
            if(parentId==-1){
              return '<h4><b>'+strName+'</b></h4>';
            }else{
              return strName;
            }
          }
        },  {
          "sTitle" : "设置日期",
          "bSortable" : false,
          "mData" : "create_time",
          "bVisible" : false,
          "mRender" : function(data, status, full) {
            var createTime = "";
            if(parentId==-1){
              return createTime;
            }else{
              createTime = data;
              return createTime;
            }
          }
        }, {
          "sTitle" : "设为默认使用",
          "bSortable" : false,
          "mData" : "default_using",
          "mRender" : function(data, status, full) {
            if(data != null && parentId != -1){
              // 如果数据库default_using为yes，则按钮为选中状态；否则为非选中状态
              if(data == "yes"){
                return '<div><input name="'+parent+'" class="only-checkbox tableCheckbox" type="radio" value="'+myId+'" checked /></div>';
              }else{
                return '<div><input name="'+parent+'" class="only-checkbox tableCheckbox" type="radio" value="'+myId+'" /></div>';
              }
            }else{
              return "";
            }
          }
        }, {
          "sTitle" : "操作",
          "bSortable" : false,
          "mData" : "status",
          "mRender" : function(data, status, full) {
            var ret = '<div class="btn-group" style="margin-top: 8px;">';
            /*if(data != null && parentId != -1){*/
              ret += '<a href="javascript:void(0);" class="btn icon-reply contrast" title="修改"></a>';
           /* }else{
              ret += "";
            }*/
            return ret += '</div>';
          }
        }],
        "fnDrawCallback" : function(oSettings) {
          // 表格绘制完成后，去除width属性，使表格可缩放
          $('#' + oSettings.sTableId).css("width", "");
          
          // 蒙版显示
          var ruleinfo = function(context) {
            loader.loading("数据加载中，请稍后……");
            var ruleinfo_init = require("./ruleinfomodal");
            if ($("#ruleinfo_id").length <= 0)
              $("body").append("<div id='ruleinfo_id'></div>");
            ruleinfo_init.init({
                  "title" : "请确认操作",
                  "renderTo" : "ruleinfo_id",
                  "details": context
            });
          };
          
          //单选按钮点击事件
          $(":radio").click(function(){
            // 获取该radio的value值
            var ruleId=$(this).val();
            
            // 显示蒙版
            ruleinfo("您确定要执行此操作吗？");
            // 保存按钮事件
            $("#btndialogconfirm").click(function(event) {
              loader.noloading();
              radioUsing = "yes";
              // 蒙版消失
              $('#my_ruleinfo_id').modal('hide');
              
              // 向后台传递的数据
              var formdata= {
                id : ruleId,
                default_using : radioUsing
              };
          
              // 执行的后台方法及渲染界面
              $.ajax({
                url :"/rule/doEditDefaultUsing.do",// GLOBAL.URL.ADMIN_RETURNLICENSECARRIER,
                dataType : "json",
                data : "data=" + JSON.stringify(formdata),
                type : "POST",
                async : false,
                success : function(json) {
                  if(json.flag == '000000'){
                    nav.toPageView("admin/ruleinfo.html");
                    loader.noloading();                   
                  } else {
                    oauth2Relogin();
                  }
                },
                error : function() {
                  loader.noloading();
                  bootbox.alertTimeout("网络请求失败，请重试！");
                }
              });
            });
            
            // 取消按钮事件
            $("#btndialogcancel").click(function(event) {
              loader.noloading();
              // 蒙版消失
              $('#my_ruleinfo_id').modal('hide');
              // 刷新界面
              exports.init(param);
            });
            
          });
        }
      });
      
      //新建规则按钮
      $("#creatbtn").on("click", function() {
        $('#rulelistDiv').hide();
        $("#ruleForm").hide();
        var accountForm = require("./ruleinfoform");
        accountForm.init("rulelist_edit", null);
        $('#rulelist_edit').show();
      });
      //修改按钮
      $("#data_table tbody").on("click", "tr>td>div>a.icon-reply", function(e) {
        $('#rulelistDiv').hide();
        $("#ruleForm").hide();
        var rowNode = this.parentNode.parentNode.parentNode;
        var rowData = ruleinfo_data_table.fnGetData(rowNode);
        var isvinforeturnform = require("./ruleinforeturnform");
        isvinforeturnform.init("rulelist_edit", rowData);
        $('#rulelist_edit').show();
      });
    });

  };
});
